home *** CD-ROM | disk | FTP | other *** search
- Path: hal.COM!spike
- From: spike@hal.com (Spike White)
- Newsgroups: comp.lang.c++
- Subject: Re: A template question...
- Date: 11 Mar 1996 20:25:13 GMT
- Organization: HAL Computer Systems, Inc.
- Message-ID: <4i2279$hes@news.hal.com>
- References: <4hkjn5$mp1@wapping.ecs.soton.ac.uk>
- NNTP-Posting-Host: zarathustra.hal.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Tony Lofthouse (tl93@ecs.soton.ac.uk) wrote:
- : I want a generic function to perform a sort on an array, so I do the
- : following:
-
- : template <class T>
- : void mysort(const T* anArray, unsigned arraysize) {
- : // implementation stuff
- : }
-
- : o.k. fine. But I also want to pass a pointer to a predicate function which
- : does the comparasion of elements in the array. It seems to me that this
- : predicate function needs to be a template function as well:
-
- : template <class T>
- : unsigned greaterthan(const T& x, const T& y) {
- : return x > y;
- : }
-
- : so my implementation of mysort is:
-
-
- : template<class T>
- : void mysort(const T* anArray, unsigned arraylen,
- : unsigned (*predicate) (const T&, constT&)) {
-
- : // sort stuff
- : if(predicate( el1, el2 ) {
- :
- : // ----
- :
- : }
- : //more sort stuff
-
- : }
-
- : Unfortunately when I do:
-
- : extern int* anArray;
- : extern unsigned anArrayLength;
-
- : mysort(anArray, anArrayLength, &greaterthan);
-
- I'm a bit of a novice, but...it all looks good to me, except your last
- line should be:
-
- mysort(anArray, anArrayLength, greaterthan);
-
- --
- Spike White | spike@hal.com | Biker Nerds
- HaL Software Systems | '87 BMW K75S, DoD #1347 | From HaL
- Austin, TX | http://www.halsoft.com/users/spike/index.html
- Disclaimer: HaL, want me to speak for you? No, Dave...
-